package com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.Fragment_persenter;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.adapter.DevicesManagerAdapter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.DevicesManagerBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.bean.LoginInfoBean;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.config.RBConfig;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.fragment_contract.IDevicesManagerContract;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.inter.IonClickedItemListenter;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.fragment.fragment_modle.DevicesManagerModle;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.JsonUitl;
import com.hzdesk.media.matvector.koreabatch.client.my_paperless.utils.RecyclerViewUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/8/8.
 * 杭州席媒科技有限公司  Developers  刘传华  10
 * 佛祖镇楼 bug辟易
 */

public class DevicesManagerPersenter implements IDevicesManagerContract.IDevicesManagerPersenter {
    private IDevicesManagerContract.IDevicesManagerView mIDevicesManagerView;
    private DevicesManagerAdapter managerAdapter;
    private String TAG = DevicesManagerPersenter.class.getSimpleName();
    private IDevicesManagerContract.IDevicesManagerModle mIDevicesManagerModle;
    private List<DevicesManagerBean> mlist;
    private Handler handler = new Handler();

    public DevicesManagerPersenter(IDevicesManagerContract.IDevicesManagerView mIDevicesManagerView) {
        this.mIDevicesManagerView = mIDevicesManagerView;
        mIDevicesManagerModle =   new DevicesManagerModle();
    }

    @Override
    public void iniTitle() {
        mIDevicesManagerView.settingTitle();
    }

    @Override
    public void initMyRecyclerView(Context mContext, RecyclerView mMyRecyclerView) {
        RecyclerViewUtils mRecyclerViewUtils =   new RecyclerViewUtils();
        managerAdapter = new DevicesManagerAdapter<DevicesManagerBean>(mContext);
        mRecyclerViewUtils.initView(mMyRecyclerView,0,mContext,managerAdapter);
        setDevicesManagerAdapterListener(managerAdapter);
    }

    @Override
    public void loadRecyclerViewDate(Context context) {
        String token = mIDevicesManagerModle.getToken(context);
      //  Log.e(TAG, "loadRecyclerViewDate: "+token );
        OkHttpUtils
                .post()
                .url(RBConfig.HOST_IP+RBConfig.DEVICES_MANAGE_LIST)
                .addHeader("sessionId",token)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String NetWorkResponse = JsonUitl.NetWorkResponse(response);
                        if(NetWorkResponse != null){
                            JSONObject object = JsonUitl.NetworkResponseDateDeal(NetWorkResponse);
                            if(object != null){
                                int codo = JsonUitl.getParsinCode(object);
                                if(codo == 100){
                                    String normalDate = JsonUitl.getStringDateFeld(object);
                                    Type type = new TypeToken<List<DevicesManagerBean>>(){}.getType();
                                    mlist = new Gson().fromJson(normalDate,type);
                                    handler.post(new Runnable() {
                                        @Override
                                        public void run() {
                                            managerAdapter.AddItemDate(mlist);
                                        }
                                    });
                                }
                            }
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    /**
     * @desc 设置监听
     * */
    private void setDevicesManagerAdapterListener(DevicesManagerAdapter managerAdapter) {
        managerAdapter.setmIonClickedItemListenter(new IonClickedItemListenter() {
            @Override
            public void onClickedItem(View view) {

            }

            @Override
            public void onClickedFist(int postion) {
                if(mlist != null && mlist.size()>0){
                    DevicesManagerBean bean =  mlist.get(postion);
                    mIDevicesManagerView.toDevicesManagerPager(bean);
                }

            }

            @Override
            public void onClickedSecond(int postion) {
                if(mlist != null && mlist.size()>0){
                    DevicesManagerBean bean =  mlist.get(postion);
                    String id = bean.getId();
                    if(id != null && !TextUtils.isEmpty(id)){
                        mIDevicesManagerView.toDevicesUndeMettingPager(id);
                    }
                }
            }
        });
    }
}
